package cn.tedu.egweibo1.base.exception;

import cn.tedu.egweibo1.base.response.JsonResult;
import cn.tedu.egweibo1.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {
    @ExceptionHandler
    public JsonResult doHandleIllegalArgumentException(IllegalArgumentException ex) {
        //获取异常处理信息
        String data = ex.getMessage();
        log.error("IllegalArgumentException : ", data);
        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }

    //父类异常处理方法
    @ExceptionHandler
    public JsonResult doHandleRuntimeException(RuntimeException ex) {
        //获取异常处理信息
        String data = ex.getMessage();
        log.error("RuntimeException : ", data);
        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }

    /**
     * Validation封装类参数数据合法性校验的异常处理方法
     */
    @ExceptionHandler
    public JsonResult doHandleMethodArgueNotValidException(MethodArgumentNotValidException ex){
        //获取异常处理信息
        String data = ex.getMessage();
        log.error("MethodArgueNotValidException : ", data);
        return new JsonResult(StatusCode.VALIDATED_ERROR,data);
    }

    //全局通用异常处理方法
//    @ExceptionHandler
//    public JsonResult doHandleThrowable(Throwable ex) {
//        //获取异常处理信息
//        String data = ex.getMessage();
//        log.error("位置异常", data);
//        return new JsonResult(StatusCode.THROWABLE_ERROR,data);
//    }
}